import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/redirect',
        children: [
            {
                path: '/redirect/:path(.*)',
                component: () => import('@/views/redirect')
            }
        ]
    },
    {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
        meta: {
            title: '首页',
            keepAlive: true,
            showTabBar: true
        }
    },
    {
        path: '/square',
        name: 'Square',
        component: () => import('@/views/notice/Square.vue'),
        meta: {
            title: '广场',
            keepAlive: true,
            showTabBar: true
        }
    },
    {
        path: '/publish',
        name: 'Publish',
        component: () => import('@/views/notice/Publish.vue'),
        meta: {
            title: '发布',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/message',
        name: 'Message',
        component: () => import('@/views/message/Message.vue'),
        meta: {
            title: '消息',
            keepAlive: true,
            showTabBar: true
        }
    },
    {
        path: '/messages/:type?',
        name: 'UnifiedMessage',
        component: () => import('@/views/message/UnifiedMessage.vue'),
        meta: {
            title: '消息详情',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/profile',
        name: 'Profile',
        component: () => import('@/views/user/Profile.vue'),
        meta: {
            title: '我的',
            keepAlive: true,
            showTabBar: true
        }
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login.vue'),
        meta: {
            title: '登录',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('@/views/Register.vue'),
        meta: {
            title: '注册',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/forgot-password',
        name: 'ForgotPassword',
        component: () => import('@/views/setting/ForgotPassword.vue'),
        meta: {
            title: '找回密码',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/search',
        name: 'Search',
        component: () => import('@/views/notice/Search.vue'),
        meta: {
            title: '搜索',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/detail/:id',
        name: 'Detail',
        component: () => import('@/views/notice/Detail.vue'),
        meta: {
            title: '详情',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/user/:id',
        name: 'User',
        component: () => import('@/views/user/User.vue'),
        meta: {
            title: '用户主页',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/settings',
        name: 'Settings',
        component: () => import('@/views/setting/Settings.vue'),
        meta: {
            title: '设置',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/profile-edit',
        name: 'ProfileEdit',
        component: () => import('@/views/user/ProfileEdit.vue'),
        meta: {
            title: '编辑个人资料',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/account-security',
        name: 'AccountSecurity',
        component: () => import('@/views/setting/AccountSecurity.vue'),
        meta: {
            title: '账号安全',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/privacy-settings',
        name: 'PrivacySettings',
        component: () => import('@/views/setting/PrivacySettings.vue'),
        meta: {
            title: '隐私设置',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/chat/:id',
        name: 'Chat',
        component: () => import('@/views/chat/Chat.vue'),
        meta: {
            title: '聊天',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/chat-detail/:id',
        name: 'ChatDetail',
        component: () => import('@/views/chat/ChatDetail.vue'),
        meta: {
            title: '聊天设置',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/message/likes',
        name: 'LikesMessage',
        redirect: '/messages/likes'
    },
    {
        path: '/message/comments',
        name: 'CommentsMessage',
        redirect: '/messages/comments'
    },
    {
        path: '/message/follows',
        name: 'FollowsMessage',
        redirect: '/messages/followers'
    },
    {
        path: '/message/system',
        name: 'SystemMessage',
        redirect: '/messages/notices'
    },
    {
        path: '/follows/:type?',
        name: 'Follows',
        component: () => import('@/views/user/Follows.vue'),
        meta: {
            title: '关注',
            keepAlive: false,
            showTabBar: false
        }
    },
    {
        path: '/follows/following',
        name: 'Following',
        redirect: '/follows/following'
    },
    {
        path: '/follows/followers',
        name: 'Followers',
        redirect: '/follows/followers'
    },
    {
        path: '/follows/mutual',
        name: 'Mutual',
        redirect: '/follows/mutual'
    },
    {
        path: '/follows/recommend',
        name: 'Recommend',
        redirect: '/follows/recommend'
    },
    // 404页面路由，使用查询参数
    {
        path: '/404',
        name: '404',
        component: () => import('@/views/error/404.vue'),
        meta: {
            title: '404 - 页面不存在',
            keepAlive: false,
            showTabBar: false
        }
    },
    // 匹配所有未定义的路由，重定向到404页面并保留原始路径
    {
        path: '*',
        redirect: to => {
            // 使用查询参数而不是路径参数
            return {
                path: '/404',
                query: {
                    source: to.path // 保留完整路径
                }
            }
        }
    }
]

const router = new VueRouter({
    mode: 'history',
    base: '/',
    routes
})

export default router